﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Xml.Linq;

using ProjectBase.Core;
using ProjectBase.Tools.Publishing;
using $safeprojectname$.Infrastructure.Factories;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace $safeprojectname$.Publishing.Providers
{
    [TestClass]
    public class IncorrectHandlerConfigurationTest : TestBase
    {
        [TestMethod]
        public void NullHandler_Passed()
        {

            Exception exception = null;
            try
            {
                var provider = ProviderFactory.WithMissingHandler() as DefaultPublishingProvider;
                var handler = provider.Handler as NullPublishingHandler;
                handler.Init(new XElement("Settings"));
                provider.Publish(this, "Exception", new Exception());
                provider.Publish(this, Severity.Warning, "Warning {0} {1}", "ABC");
                provider.Publish(this, () => "Info {0} {1}".FormatWith("ABC"), Severity.Info);
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            Assert.IsTrue(exception.IsNull());
        }

        [TestMethod]
        public void IncorrectHandler_Failed()
        {
            Exception exception = null;
            try
            {
                var provider = ProviderFactory.WithIncorrectHandler() as DefaultPublishingProvider;
            }
            catch (ConfigurationErrorsException ex)
            {
                exception = ex;
            }
            Assert.IsTrue(exception.Message.Contains("Publishing HandlerConfigurator: handler type"), exception.Message);
            Assert.IsTrue(exception.Message.Contains("does not exist."), exception.Message);
        }

        [TestMethod]
        public void BrokenHandler_Failed()
        {
            Exception exception = null;
            try
            {
                var provider = ProviderFactory.WithBrokenHandler() as DefaultPublishingProvider;
            }
            catch (ConfigurationErrorsException ex)
            {
                exception = ex;
            }
            Assert.IsTrue(exception.Is());
        }


        #region Init

        // You can use the following additional attributes as you write your tests:

        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }

        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }

        [TestInitialize]
        public override void MyTestInitialize()
        {
            base.MyTestInitialize(); // Use TestInitialize to run code before running each test
        }

        [TestCleanup]
        public override void MyTestCleanup()
        {
            base.MyTestCleanup(); // Use TestCleanup to run code after each test has run
        }
        #endregion Init
    }
}
